<?php

namespace app\controllers\api;

use app\models\UserModel;
use app\controllers\ApiController;


class LoginController extends ApiController
{
    public function login()
    {
        $params = $_POST;
        $arFail = ['status'=>'error', 'msg'=>'请求失败'];
        if (empty($params['username']) || empty($params['password'])) {
            $this->json($arFail);
        }

        $sName = $params['username'];
        $sPass = $params['password'];

        $UserModel = new UserModel();
        $row = $UserModel->getByNamePass($sName, $sPass);

        if (!$row) {
            $this->json($arFail);
        }

        $arRes = ['status'=>'ok', 'data'=>['token'=>$row['token']]];
        if (strtotime($row['token_valid']) < time()) {
            $UserModel->updateToken($row['id']);
        }
        $this->json($arRes);
    }

    public function logout()
    {
        $this->json(['status'=>'ok']);
    }

}